home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / utility1 / gs261src.zip / GSLINE.C < prev    next >
C/C++ Source or Header  |  1993-05-13  |  5KB  |  156 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsline.c */
  20. /* Line parameter operators for Ghostscript library */
  21. #include "math_.h"
  22. #include "memory_.h"
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"            /* ditto */
  26. #include "gxmatrix.h"            /* for gzstate */
  27. #include "gzstate.h"
  28. #include "gzline.h"
  29.  
  30. /* setlinewidth */
  31. int
  32. gs_setlinewidth(gs_state *pgs, floatp width)
  33. {    pgs->line_params->width = width / 2;
  34.     return 0;
  35. }
  36.  
  37. /* currentlinewidth */
  38. float
  39. gs_currentlinewidth(const gs_state *pgs)
  40. {    return (float)(pgs->line_params->width * 2);
  41. }
  42.  
  43. /* setlinecap */
  44. int
  45. gs_setlinecap(gs_state *pgs, gs_line_cap cap)
  46. {    pgs->line_params->cap = cap;
  47.     return 0;
  48. }
  49.  
  50. /* currentlinecap */
  51. gs_line_cap
  52. gs_currentlinecap(const gs_state *pgs)
  53. {    return pgs->line_params->cap;
  54. }
  55.  
  56. /* setlinejoin */
  57. int
  58. gs_setlinejoin(gs_state *pgs, gs_line_join join)
  59. {    pgs->line_params->join = join;
  60.     return 0;
  61. }
  62.  
  63. /* currentlinejoin */
  64. gs_line_join
  65. gs_currentlinejoin(const gs_state *pgs)
  66. {    return pgs->line_params->join;
  67. }
  68.  
  69. /* setmiterlimit */
  70. int
  71. gs_setmiterlimit(gs_state *pgs, floatp limit)
  72. {    if ( limit < 1.0 ) return_error(gs_error_rangecheck);
  73.     pgs->line_params->miter_limit = limit;
  74.     /* The supplied miter limit is an upper bound on */
  75.     /* 1/sin(phi/2).  We convert this to a lower bound on */
  76.     /* tan(phi).  Note that if phi > pi/2, this is negative. */
  77.     /* We use the half-angle and angle-sum formulas here */
  78.     /* to avoid the trig functions.... */
  79.        {    double limit_sq = limit * limit;
  80.     /* We need a special check for phi/2 close to pi/4. */
  81.     /* Some C compilers can't handle the following as a */
  82.     /* conditional expression.... */
  83.         if ( limit_sq < 2.0001 && limit_sq > 1.9999 )
  84.             pgs->line_params->miter_check = 1.0e6;
  85.         else
  86.             pgs->line_params->miter_check =
  87.                 sqrt(limit_sq - 1) * 2 / (limit_sq - 2);
  88.        }
  89.     return 0;
  90. }
  91.  
  92. /* currentmiterlimit */
  93. float
  94. gs_currentmiterlimit(const gs_state *pgs)
  95. {    return pgs->line_params->miter_limit;
  96. }
  97.  
  98. /* setdash */
  99. int
  100. gs_setdash(gs_state *pgs, const float *pattern, uint length, floatp offset)
  101. {    uint n = length;
  102.     const float *dfrom = pattern;
  103.     char ink = 1;
  104.     int index = 0;
  105.     float pattern_length = 0.0;
  106.     float dist_left;
  107.     dash_params *dash = &pgs->line_params->dash;
  108.     float *ppat;
  109.     /* Check the dash pattern */
  110.     while ( n-- )
  111.        {    float elt = *dfrom++;
  112.         if ( elt < 0 ) return_error(gs_error_rangecheck);
  113.         pattern_length += elt;
  114.        }
  115.     if ( length == 0 )        /* empty pattern */
  116.        {    dist_left = 0.0;
  117.         ppat = 0;
  118.        }
  119.     else
  120.        {    if ( pattern_length == 0 )
  121.             return_error(gs_error_rangecheck);
  122.         /* Compute the initial index, ink_on, and distance left */
  123.         /* in the pattern, according to the offset. */
  124. #define f_mod(a, b) ((a) - floor((a) / (b)) * (b))
  125.         dist_left = f_mod(offset, pattern_length);
  126.         while ( (dist_left -= pattern[index]) >= 0 )
  127.             ink = !ink, index++;
  128.         ppat = (float *)gs_malloc(length, sizeof(float),
  129.                       "dash pattern");
  130.         if ( ppat == 0 ) return_error(gs_error_VMerror);
  131.         memcpy(ppat, pattern, length * sizeof(float));
  132.        }
  133.     dash->pattern = ppat;
  134.     dash->pattern_size = length;
  135.     dash->offset = offset;
  136.     dash->init_ink_on = ink;
  137.     dash->init_index = index;
  138.     dash->init_dist_left = -dist_left;
  139.     return 0;
  140. }
  141.  
  142. /* currentdash */
  143. uint
  144. gs_currentdash_length(const gs_state *pgs)
  145. {    return pgs->line_params->dash.pattern_size;
  146. }
  147. int
  148. gs_currentdash_pattern(const gs_state *pgs, float *pattern)
  149. {    memcpy(pattern, pgs->line_params->dash.pattern, pgs->line_params->dash.pattern_size * sizeof(float));
  150.     return 0;
  151. }
  152. float
  153. gs_currentdash_offset(const gs_state *pgs)
  154. {    return pgs->line_params->dash.offset;
  155. }
  156.